package SignAndRegister;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Objects;

public class SignWindow extends JFrame{

    //作为成员变量，便于构造方法应用和监听器的应用
    private JButton signButton = new JButton("登录");
    private JButton registerButton = new JButton("注册");
    private JTextField iDText = new JTextField();
    private JPasswordField codeText = new JPasswordField();
    private JComboBox typeChoice = new JComboBox<>();
    private JRadioButton viewOrHide = new JRadioButton("显示密码");
    private boolean isView = false;
    private JLabel firstStatement = new JLabel("用户ID是什么？");
    private JLabel secondStatement = new JLabel("若您是住客，用户名为手机号；");
    private JLabel thirdStatement = new JLabel("若您是本酒店职工或管理员，用户名为工号。");
    private JLabel forthStatement = new JLabel("如何找回密码？");
    private JLabel fifthStatement = new JLabel("请联系系统管理员为您重置密码，联系方式：1581815xxxx");
    //声明页面内容监听器
    private  SignWindowHandler signWindowHandler = new SignWindowHandler(this);

    public SignWindow(){

        JPanel signPanel = new JPanel();
        this.add(signPanel);
        signPanel.setLayout(null);
        placeComponents(signPanel);

        signButton.setActionCommand("sign");
        registerButton.setActionCommand("register");
        signButton.addActionListener(signWindowHandler);
        registerButton.addActionListener(signWindowHandler);
        codeText.setEchoChar('*');

        //限制非法输入
        iDText.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                int keyChar = e.getKeyChar();
                if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){

                }else{
                    e.consume();
                }
            }

        });

        Font defaultFont = signPanel.getFont();
        Font font = new Font(defaultFont.getFamily(),defaultFont.getStyle(),100);
        this.setFont(font);

        //设置登录界面的一些参数：背景颜色、标题、大小、位置、是否可调大小、是否可见
        this.setBackground(Color.white);
        setTitle("登录界面");
        setBounds(0, 0, 800, 530);
        setLocationRelativeTo(null);//居中显示
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  //用户单击窗口的关闭按钮时程序执行的操作
        setResizable(false);
        setVisible(true);

    }

    private void placeComponents(JPanel signPanel){

        int labelDivWidth = 100;
        int labelDivHeight = 230;

        signPanel.setLayout(null);

        ImageIcon welcomeImageIcon = new ImageIcon(getClass().getResource("/image/welcome.png"));   //?
        int width = (int)(welcomeImageIcon.getIconWidth()*0.4);
        int height = (int)(welcomeImageIcon.getIconHeight()*0.4);
        Image newImage = welcomeImageIcon.getImage().getScaledInstance(width,height,Image.SCALE_DEFAULT);;
        JLabel welcomeLabel = new JLabel(new ImageIcon(newImage),SwingConstants.CENTER);

        JLabel iDLabel = new JLabel("账户ID:");
        JLabel codeLabel = new JLabel("账户密码:");
        JLabel typeLabel = new JLabel("账户类型:");

        typeChoice.addItem("住客");
        typeChoice.addItem("职工");
        typeChoice.addItem("酒店管理员");
        typeChoice.addItem("系统管理员");

        welcomeLabel.setBounds(0,-140,800,530);
        iDLabel.setBounds(labelDivWidth,labelDivHeight+20,80,25);
        iDText.setBounds(labelDivWidth+70,labelDivHeight+20,165,25);
        codeLabel.setBounds(labelDivWidth,labelDivHeight+50,80,25);
        codeText.setBounds(labelDivWidth+70,labelDivHeight+50,165,25);
        typeLabel.setBounds(labelDivWidth,labelDivHeight+80,80,25);
        typeChoice.setBounds(labelDivWidth+70,labelDivHeight+80,165,25);
        signButton.setBounds(labelDivWidth,labelDivHeight+110,235,25);
        registerButton.setBounds(labelDivWidth+155,labelDivHeight+140,80,25);
        viewOrHide.setBounds(labelDivWidth,labelDivHeight+140,80,25);

        firstStatement.setBounds(labelDivWidth+290,labelDivHeight+20,200,25);
        secondStatement.setBounds(labelDivWidth+320,labelDivHeight+50,400,25);
        thirdStatement.setBounds(labelDivWidth+320,labelDivHeight+80,400,25);
        forthStatement.setBounds(labelDivWidth+290,labelDivHeight+110,200,25);
        fifthStatement.setBounds(labelDivWidth+320,labelDivHeight+140,400,25);

        viewOrHide.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(isView){
                    codeText.setEchoChar('*');
                    isView = false;
                }else{
                    codeText.setEchoChar((char) 0);
                    isView = true;
                }
            }
        });

        signPanel.add(welcomeLabel);
        signPanel.add(iDLabel);
        signPanel.add(iDText);
        signPanel.add(codeLabel);
        signPanel.add(codeText);
        signPanel.add(typeLabel);
        signPanel.add(typeChoice);
        signPanel.add(signButton);
        signPanel.add(registerButton);
        signPanel.add(viewOrHide);

        signPanel.add(firstStatement);
        signPanel.add(secondStatement);
        signPanel.add(thirdStatement);
        signPanel.add(forthStatement);
        signPanel.add(fifthStatement);
    }

    public String getIDText(){
        return iDText.getText();
    }

    public String getCodeText(){
        return codeText.getText();
    }

    public String getTypeChoice(){
        return typeChoice.getSelectedItem().toString();
    }

}
